app.service('memberCardUpdateService',['$q','utilService','$stateParams',function($q,utilService,$stateParams){

    //根据ID查找会员卡信息
    this.findMemberCard=function($scope){
        var deferred = $q.defer();
        utilService.get({
            url:$scope.app.domain + 'memberCard/find',
            params:{cardId:$stateParams.id}
        }).then(function(response){
            deferred.resolve(response.data);
        },function(e){
            console.error(e);
            deferred.reject("连接服务器出错");
        });
        return deferred.promise;
    };

    //更改会员卡信息
    this.updateMemberCard=function($scope){

        if($scope.memberCard.type==0 || $scope.memberCard.type==2){
            delete $scope.memberCard.buyCount;
            delete $scope.memberCard.consumePrice;
            delete $scope.memberCard.consumeIntegral;
            //delete $scope.memberCard.grade;
        }

        //if($scope.memberCard.activationState==0){
        //    delete $scope.memberCard.isPerfectMessage;
        //    delete $scope.memberCard.isPhoneVerification;
        //}

        if($scope.memberCard.isDiscount==0){
            delete $scope.memberCard.discount;
        }

        if($scope.memberCard.isOpenConsumePrice==0){
            delete $scope.memberCard.consumePrice;
        }

        if($scope.memberCard.isOpenBuyCount==0){
            delete $scope.memberCard.buyCount;
        }

        if($scope.memberCard.isOpenConsumeIntegral==0){
            delete $scope.memberCard.consumeIntegral;
        }

        if($scope.memberCard.isPresentCoupon==0){
            delete $scope.memberCard.couponsId;
            //delete $scope.memberCard.couponsCount;
        }

        if($scope.memberCard.validTimeState==0){
            delete $scope.memberCard.validDays;
            delete $scope.memberCard.validStartTime;
            delete $scope.memberCard.validEndTime;
        }else if($scope.memberCard.validTimeState==1){
            delete $scope.memberCard.validDays;
        }else if($scope.memberCard.validTimeState==2){
            delete $scope.memberCard.validStartTime;
            delete $scope.memberCard.validEndTime;
        }
        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain+"memberCard/update",
            data:$scope.memberCard,
            method:'PUT'
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;
    };

    this.findCoupons=function($scope){
        var deferred = $q.defer();
        utilService.get({
            url:$scope.app.domain + 'coupons/list',
            params:$scope.coupons
        }).then(function(response){
            deferred.resolve(response.data);
        },function(e){
            console.error(e);
            deferred.reject("连接服务器出错");
        });
        return deferred.promise;
    }

    this.getValueScope=function($scope){
        var deferred = $q.defer();
        utilService.get({
            url:$scope.app.domain + 'memberCard/scope',
            params:{id:$stateParams.id}
        }).then(function(response){
            deferred.resolve(response.data);
        },function(e){
            console.error(e);
            deferred.reject("连接服务器出错");
        });
        return deferred.promise;
    };
}]);